package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.api.service.CPReportService;
import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.ReceiveIntentionService;
import com.yixun.qfbao.apidto.ReceiveIntentionReqDto;
import com.yixun.qfbao.apidto.ReceiveIntentionResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.vo.ReceiveIntentionReqVo;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: channel-parent
 *
 * @description: V10同步报备检查接口
 *
 * @author: Seven.wang
 *
 * @create: 2019-12-02 10:56
 **/

@Service("receiveIntentionCheckHander")
public class ReceiveIntentionCheckHander extends CommonAbstractHander {

    private Logger logger = LoggerFactory.getLogger(ReceiveIntentionCheckHander.class);

    @Autowired
    private PremisesService premisesService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;
    @Autowired
    ReceiveIntentionService receiveIntentionService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<ReceiveIntentionResDto> res = new BaseResponseDTO<ReceiveIntentionResDto>();
        ReceiveIntentionResDto resDto = new ReceiveIntentionResDto();
        ReceiveIntentionReqVo reqVo = (ReceiveIntentionReqVo)reqDto;
        ReceiveIntentionReqDto receiveIntentionReqDto = reqVo.getParams();
        // 1.验证参数
        if(receiveIntentionReqDto.getEmployeeUuid() == null){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10报备人id为空");
            return res;
        }
        if(receiveIntentionReqDto.getEmployeePhone() == null){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10用户手机为空");
            return res;
        }
        if(receiveIntentionReqDto.getPremisesId() == null){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10报备人id为空");
            return res;
        }
        if(receiveIntentionReqDto.getReportVisit() == null){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("约看信息不能为空");
            return res;
        }
        try{
            String companyUuid = null;
            String agencyToken = null;
            String assistantToken = null;
            Premises premises = premisesService.selectSimpleById(Long.valueOf(receiveIntentionReqDto.getPremisesId()));
            logger.info("V10报备校验,获取消息推送信息 premises info:", JSONObject.toJSONString(premises));
            if(null != premises){
                Company company = companyService.getCompanyById(premises.getCompanyId());
                logger.info("V10报备校验,获取消息推送信息 company info", JSONObject.toJSONString(company));
                if(null != company){
                    companyUuid = company.getCompanyUuid();
                }
            }
            if(StringUtils.isNotEmpty(companyUuid)){
                logger.info("V10报备校验,获取消息推送信息 companyUuid", companyUuid);
                agencyToken = wxAccessTokenCacheUtils.getAccessToken(companyUuid, SystemIdEnum.AGENCY_h5.getCode(), null);
                logger.info("V10报备校验,获取消息推送信息 agencyToken", companyUuid);
                assistantToken = wxAccessTokenCacheUtils.getAccessToken(companyUuid, SystemIdEnum.ASSISTANT.getCode(), null);
            }

            logger.info("V10报备校验,获取消息推送信息CompanyUuid: "+companyUuid +" assistantToken:" + assistantToken+" agencyToken:"+agencyToken);
            resDto = receiveIntentionService.receiveIntentionCheck(receiveIntentionReqDto,companyUuid);
        }catch (Exception e){
            logger.error("ReceiveIntentionHander error: {}", e);
            resDto.setResult("4");
            resDto.setResultMsg(e.getMessage());
        }
        res.setResult(resDto.getResult());
        res.setErrorMsg(resDto.getResultMsg());
        res.setResData(resDto);
        return res;
    }
}
